window.dontAddNextLoadToHistory=false;

function doNavigate(evt){try{
	if(evt.clientX<32){
		historyBack();
	}else if(evt.clientX<64){
		historyFwd();
	}
}catch(e){trace(e);}}

function historyBack(){try{
	if(tabs[activeTab].tab.webNavigation.canGoBack)
		tabs[activeTab].tab.webNavigation.goBack();
}catch(e){trace(e);}}


function historyFwd(){try{
	if(tabs[activeTab].tab.webNavigation.canGoForward)
		tabs[activeTab].tab.webNavigation.goForward();
}catch(e){trace(e);}}

function onLoad(evt){try{
	showTitles();
	var url=Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(this.contentWindow.location,null,null);
	Cc["@mozilla.org/browser/nav-history-service;1"].getService(Ci.nsIGlobalHistory2).addPageWithDetails(url,false,true,null);
}catch(e){trace(e);}}

function searchHistory(text){try{
	var historyService = Cc["@mozilla.org/browser/nav-history-service;1"].getService(Components.interfaces.nsINavHistoryService);
	var q=historyService.getNewQuery();
	if(text)
		q.searchTerms=text;
	var o=historyService.getNewQueryOptions();
	o.sortingMode=18;
	var r=historyService.executeQuery(q,o);
	var c = r.root;
	c.containerOpen = true;
	var m="";
	for (var i = 0; i < c.childCount; i ++) {
	    var n = c.getChild(i);
	    m+=(n.uri+" "+(n.title!="false"?n.title:"")+ "\n");
	}
	c.containerOpen = false;
	alert(m);
}catch(e){trace(e);}}

window.restoreTable=null;
function openRestoreTable(){try{
	var db=window.dataServices.openTable("restoreData","restoreTable","url STRING, title STRING");
	window.restoreTable=db;
	
	return db;
}catch(e){trace(e);}}

function clearRestoreTable(){try{
	if(!window.restoreTable)
		openRestoreTable();
	
	window.restoreTable.executeSimpleSQL("DELETE FROM restoreTable");
}catch(e){trace(e);}}

function writeCurrentTabs(){try{
	if(!window.restoreTable)
		openRestoreTable();
		
	clearRestoreTable();
	
	var writeStatement=window.restoreTable.createStatement("INSERT INTO restoreTable VALUES(:url,:title)");
	for(var i=0;i<tabs.length;i++){
		writeStatement.reset();
		writeStatement.params.url=tabs[i].tab.contentWindow.location.toString();
		writeStatement.params.title=tabs[i].tab.contentWindow.document.title.toString();
		writeStatement.executeStep();
	}
}catch(e){trace(e);}}

function getSavedTabs(){try{
	if(!window.restoreTable)
		openRestoreTable();
		
	var getStatement=window.restoreTable.createStatement("SELECT * FROM restoreTable");
	var result=[];
	while (getStatement.executeStep()) {
		result.push({url:getStatement.row.url,title:getStatement.row.title});
	}
	
	return result;
}catch(e){trace(e);}}